﻿using IMES.Common.Domain.SirModel;
using IMES.Common.Dtos;
using IMES.Common.Infrastructure;
using MediatR;

namespace GlennQuigmire.API.Application.Commands
{
    public class OutboundOrderCreateHander : IRequestHandler<OutboundOrderCreateCommand, Result>
    {

        private readonly IBaseRepositorys<OutboundOrder> outboundOrderRepository;
        private readonly IBaseRepositorys<OutboundOrderType> outboundOrderTypeRepository;
        private readonly IBaseRepositorys<OutboundOrderProduct> outboundOrderDetailRepository;


        public OutboundOrderCreateHander(IBaseRepositorys<OutboundOrder> outboundOrderRepository, IBaseRepositorys<OutboundOrderType> outboundOrderTypeRepository, IBaseRepositorys<OutboundOrderProduct> outboundOrderDetailRepository)
        {
            this.outboundOrderRepository = outboundOrderRepository;
            this.outboundOrderTypeRepository = outboundOrderTypeRepository;
            this.outboundOrderDetailRepository = outboundOrderDetailRepository;
        }

        /// <summary>
        /// 创建出库单
        /// </summary>
        /// <param name="request">出库单添加信息</param>
        /// <param name="cancellationToken">允许取消的Token</param>
        /// <returns>返回响应对象</returns>
        public Task<Result> Handle(OutboundOrderCreateCommand request, CancellationToken cancellationToken)
        {
            try
            {
                var result = new Result();
                OutboundOrder outboundOrder = new OutboundOrder();
                outboundOrder.OutboundOrderNumber = request.OutboundOrderNumber;
                outboundOrder.OutboundOrderTypeId = request.OutboundOrderTypeId;
                outboundOrder.OutbroundOrderDate = request.OutbroundOrderDate;
                outboundOrder.FileUrl= request.FileUrl;
                outboundOrder.ImageUrl = request.ImageUrl;
                outboundOrder.ExamineStatus= request.ExamineStatus;
                outboundOrder.Remark= request.Remark;
                outboundOrder.ExamineRemark= request.ExamineRemark;
                outboundOrder.CreateId = request.CreateId;
                outboundOrder.CreateDate = DateTime.Now;
                outboundOrder.CreateName = request.CreateName;
                int n = outboundOrderRepository.Add(outboundOrder);
                if (n > 0)
                {
                    int outboundOrderId = outboundOrderRepository.GetAll().FirstOrDefault(c => c.OutboundOrderNumber == request.OutboundOrderNumber).OutboundOrderId;

                    foreach (var item in request.OutboundOrderCreateDtos)
                    {
                        OutboundOrderProduct outboundOrderProduct = new OutboundOrderProduct();
                        outboundOrderProduct.OutboundOrderId = outboundOrderId;
                        outboundOrderProduct.ProductId = item.ProductId;
                        outboundOrderProduct.OutNum = item.OutNum;
                         int m= outboundOrderDetailRepository.Add(outboundOrderProduct);
                        if (m > 0)
                        {
                            n++;
                        }
                    }

                }
                result.Code = n;
                if(n==(1+request.OutboundOrderCreateDtos.Count()))
                {
                    result.Messege = "创建成功";
                }
                else
                {
                    result.Messege = "创建失败";
                }

                return Task.FromResult(result);
            }
            catch(Exception ex)
            {
                throw ex;
            }
        }
    }
}
